#include<iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>

using namespace std;

int main()
{
    //1.创建socket
    int ckt = socket( AF_INET, SOCK_STREAM, 0);

    //2.链接服务器，connet
    struct sockaddr_in serveaddr;
    memset(&serveaddr, 0, sizeof(serveaddr));
    serveaddr.sin_family = AF_INET;//TCP/IP协议族
    serveaddr.sin_port = htons(8023);  //服务器端口
    serveaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    if (connect(ckt, (struct sockaddr *)&serveaddr, sizeof(serveaddr)) == -1) {
        perror("connect");
        cout<<"错误出现在connect阶段"<<endl;
        exit(1);
    }
    cout << "连接服务器成功！\n";

    //3.接收/发送消息，recv/send
    char sendbuffer[1024];

    while(1) {
        memset(sendbuffer, 0, sizeof(sendbuffer));        
        cout << "请输入要发送的信息：";
        cin >> sendbuffer;
        send(ckt, sendbuffer, strlen(sendbuffer), 0); //发送
        if(strcmp(sendbuffer, "exit") == 0)
            break;
    }

    //4.关闭链接，close
    close(ckt);

    return 0;
}